package cn.dujc.superkid.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

import cn.dujc.superkid.R;
import cn.dujc.superkid.bean.CupIndexResults;

public class MatchFragmentRightAdapter extends RecyclerView.Adapter<MatchFragmentRightAdapter.MyHolder> {

    private static final String TAG = "CourseFragmentLeftAdapt";
    List<CupIndexResults.DataBean.CupBean> cup;
    private Context context;
    private MatchFragmentRightAdapter.OnRightItemClickListener onRightItemClickListener;

    public MatchFragmentRightAdapter(List<CupIndexResults.DataBean.CupBean> cup, Context context, MatchFragmentRightAdapter.OnRightItemClickListener onRightItemClickListener) {
        this.cup = cup;
        this.context = context;
        this.onRightItemClickListener = onRightItemClickListener;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LinearLayout view = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.item_rv_course_fragment_right, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        CupIndexResults.DataBean.CupBean cupBean = cup.get(position);
        if (holder != null) {
            if (cupBean==null||cupBean.getCup_name().equals("null")){
            }else {
                String cup_name = cupBean.getCup_name();
                holder.tvContentCourse.setText(cup_name);
                Log.d(TAG, "onBindViewHolder: k_name=" + cup_name);
                final List<CupIndexResults.DataBean.CupBean.ClistBean> cuplist = cupBean.getClist();
                final LayoutInflater mInflater = LayoutInflater.from(context);
                holder.flowLayout.setAdapter(new TagAdapter<CupIndexResults.DataBean.CupBean.ClistBean>(cuplist) {
                    @Override
                    public View getView(FlowLayout parent, final int position, CupIndexResults.DataBean.CupBean.ClistBean cuplistBean) {
                        TextView tv = (TextView) mInflater.inflate(R.layout.tv_right_flow_layout,
                                holder.flowLayout , false);
                        int height=tv.getHeight();
                        tv.setText(cuplistBean.getTitle());
                        int height1=tv.getHeight();
                        if(tv.getLineCount()>1){
                            String text=cuplistBean.getTitle().substring(0,9)+"..."+cuplistBean.getTitle().substring(13);
                            tv.setText(text);
                        }
                        tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                onRightItemClickListener.onRightItemClickListener(cuplist.get(position));
                            }
                        });
                        return tv;
                    }
                });
            }
            }
    }

    @Override
    public int getItemCount() {
        Log.d(TAG, "getItemCount: item个数=" + cup.size());
        return cup.size();
    }

    public  class MyHolder extends RecyclerView.ViewHolder {
        TextView tvContentCourse;
        TagFlowLayout flowLayout;
        public MyHolder(View itemView) {
            super(itemView);
            tvContentCourse = itemView.findViewById(R.id.tv_content_course);
            flowLayout = itemView.findViewById(R.id.flow_layout);
        }
    }

    public interface OnRightItemClickListener {
        void onRightItemClickListener(CupIndexResults.DataBean.CupBean.ClistBean clistBean);
    }
}
